package com.team.party.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.team.party.bean.Society;
import com.team.party.service.ManagerService;
import com.team.party.service.impl.ManagerServiceImpl;

/**
 * Servlet implementation class UploadServlet
 */
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
	@Autowired
	ManagerService managerService;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		SmartUpload su = new SmartUpload();
		su.initialize(getServletConfig(), request, response);
		su.setAllowedFilesList("jpg,jpeg,png,gif,bmp,JPG,JPEG,PNG,GIF,BMP");
		su.setMaxFileSize(1024*1024*10);
		su.setTotalMaxFileSize(1024*1024*50);
		try {
			su.upload();
		} catch (SmartUploadException e) {
				// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Files files = su.getFiles();
		File file=files.getFile(0);
		//将上传的文件的名称重新命名为时间+扩展名
		Date date = new Date();
		String date_time = date.getTime()+"";
		String imgPath = "F:\\Project_PGY\\STS\\party\\src\\main\\resources\\static\\img_active\\"+date_time+"."+file.getFileExt();
		
		System.out.println("-----------------");
		System.out.println(imgPath);
		System.out.println("-----------------");
		
		
		
		try {
			file.saveAs(imgPath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Request req =su.getRequest();
	
		HttpSession session = request.getSession();
		
		Society society = (Society) session.getAttribute("loginManager");
		System.out.println(society);
		
		String soc_no = society.getSoc_no();
		String act_name = req.getParameter("act_name");
		String act_loc = req.getParameter("act_loc");
		String act_no = req.getParameter("act_no");
		String act_start = req.getParameter("act_start");
		String act_end = req.getParameter("act_end");
		int act_public = 1;
		String act_intro = req.getParameter("act_intro");
		
		
		String act_pic = "../img_active/"+date_time+"."+file.getFileExt();
		
		System.out.println(soc_no);
		System.out.println(act_name);
		System.out.println(act_loc);
		System.out.println(act_no);
		System.out.println(act_start);
		System.out.println(act_end);
		System.out.println(act_intro);
		System.out.println(act_pic);
		
		
		
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		Date d_act_start = new Date();
		Date d_act_end = new Date();
		
		try {
			d_act_start = format.parse(act_start);
			d_act_end = format.parse(act_end);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.err.println("时间：");
		System.out.println(d_act_start);
		System.out.println(d_act_end);
		
		System.out.println("校验：");
		System.out.println(act_no);
		System.out.println(soc_no);
		System.out.println(act_name);
		System.out.println(act_loc);
		System.out.println(d_act_start);
		System.out.println(d_act_end);
		System.out.println(act_public);
		System.out.println(act_intro);
		System.out.println(act_pic);
    	
		Boolean b = false;
    	try {
			b = managerService.saveActivity(act_no, soc_no, act_name, act_loc, d_act_start, d_act_end, act_public, act_intro, act_pic);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
    	if(b) {
    		System.out.println("添加成功");
    	} else {
    		System.out.println("添加失败");
    	}
    	
    	//成功后跳转页面
		response.sendRedirect("party-active.html");
	}

}
